package de.freudenreichdesign.webshop.model;

import java.util.HashSet;
import java.util.Set;

public class ShoppingCart {
	
	private Set<ShoppingCartItem> listOfArticles = new HashSet<ShoppingCartItem>();

	public ShoppingCart() {}
	
	public Set<ShoppingCartItem> getListOfArticles() {
		return listOfArticles;
	}
	
	public void addItem(ShoppingCartItem item) {
		listOfArticles.add(item);
	}
	
	public void removeItem(int position) {
		if (position >= 0)
			listOfArticles.remove(position);
	}
	
	public ShoppingCartItem getItem(int position) {
		if (position >= 0)
			return (ShoppingCartItem) listOfArticles.toArray()[position];
		else
			return null;
	}
	
	public void removeItemByArticleID(int articleID) {
		if (!listOfArticles.isEmpty()){
			int position = -1;
			for (int i = 0; i < listOfArticles.size(); i++) {
				if (((ShoppingCartItem) listOfArticles.toArray()[i]).getArticle().getArticleID() == articleID) 
					position = i;
			}
			removeItem(position);
		}
	}
	
	public ShoppingCartItem getItemByArticleID(int articleID) {
		if (!listOfArticles.isEmpty()){
			int position = -1;
			for (int i = 0; i < listOfArticles.size(); i++) {
				if (((ShoppingCartItem) listOfArticles.toArray()[i]).getArticle().getArticleID() == articleID) 
					position = i;
				}
			return getItem(position);
		} else
			return null;
	}
	
	public double getPrice() {
		double price = 0.0;
		for (ShoppingCartItem shoppingCardItem : listOfArticles) {
			price += shoppingCardItem.getTotal();
		}
		return price;
	}
	
	public void clearShoppingCart() {
		listOfArticles.clear();
	}
	
	public int numberOfArticles() {
		int number = 0;
		for (ShoppingCartItem shoppingCardItem : listOfArticles) {
			number += shoppingCardItem.getUnits();
		}
		return number;
	}
}
